# -*- coding: utf-8 -*-

import requests
import json
import logging
from pprint import pprint
from util.constants import Constants


###############################
#  工具类
###############################

class Utils():

    def __init__(self):
        pass

    # post 请求封装
    @classmethod
    def post(self, contentType, clientId, clientSecret, authorization, url, data):
        header = {
            'Content-Type': contentType,
            'client_id': clientId,
            'client_secret': clientSecret,
            'Authorization': authorization
        }
        r = requests.post(url,
                          data=data,
                          headers=header)
        return r

    # get 请求封装
    @classmethod
    def get(self, contentType, clientId, clientSecret, authorization, url, data):
        header = {
            'Content-Type': contentType,
            'client_id': clientId,
            'client_secret': clientSecret,
            'Authorization': authorization
        }
        r = requests.get(url,
                         data=data,
                         headers=header)
        return r

    # 对结果解析获取data内容
    @classmethod
    def getReturnData(self, r):
        if r.status_code == 200:
            d = json.loads(r.text)
            if d == None:
                logging.error("接口返回异常")
                return None
            else:
                code = d['resp_code']
                if code == 0:
                    return d['data']
                else:
                    logging.error("接口返回异常,code=", code)
                    return None
        else:
            logging.error("请求服务异常，status_code=", r.status_code)
            return None

    # 获取结果code值
    @classmethod
    def getReturnCode(self, r):
        if r.status_code == 200:
            d = json.loads(r.text)
            if d == None:
                logging.error("接口返回异常")
                return None
            else:
                code = d['resp_code']
                return code
        else:
            logging.error("请求服务异常，status_code=", r.status_code)
            return None

    # 获取token
    @classmethod
    def getToken(self, loginType, data):
        # CLIENT_TYPE_APP = "app"
        # CLIENT_TYPE_ADMIN_WEB_APP = "webApp"
        # CLIENT_TYPE_SUPPLIER_WEB_APP = "supplierApp"
        # CLIENT_TYPE_DATAVIEW = "dataView"
        # CLIENT_TYPE_CRM_WEB_APP = "crmWebApp"
        # CLIENT_TYPE_CRM_APP = "crmApp"

        if data == None or loginType == None:
            return None

        tokenData = {}
        if loginType == Constants.CLIENT_TYPE_APP:
            tokenData = data['token']
        elif loginType == Constants.CLIENT_TYPE_ADMIN_WEB_APP:
            tokenData = data
        elif loginType == Constants.CLIENT_TYPE_SUPPLIER_WEB_APP:
            tokenData = data
        elif loginType == Constants.CLIENT_TYPE_CRM_WEB_APP:
            tokenData = data
        elif loginType == Constants.CLIENT_TYPE_CRM_APP:
            tokenData = data

        if not tokenData:
            pprint('tokenData is error')
            return ''

        return "Bearer " + tokenData['access_token']

    # 获取供应商ID
    @classmethod
    def getSupplierLoginId(self, data):
        if data == None:
            return None
        return data['supplierId']


if __name__ == '__main__':
    # 测试
    # headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
    #            'client_id': 'supplierApp',
    #            'client_secret': 'supplierApp',
    #            'Authorization': 'Bearer e64c5b5e-7044-4fbe-84ff-4cdefc956db0', }
    # r = requests.post('http://test.shop.kyaoduo.com/api-user/users/supplier/current',
    #                   headers=headers,
    #                   data=None)
    # pprint(r.text)

    headers = None
    data = {
        'username': 'admin',
        'password': '0000'
    }
    r = requests.post('http://60.174.109.246:7980/Api/Login',
                      headers=headers,
                      data=data)
    pprint(r.text)
